<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <meta charset="UTF-8">
    <title>采购退货详情 - [[${T(com.b2c.erp.DataConfigObject).getInstance().getSysName()}]]</title>

    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <script src="/js/jquery-3.3.1.min.js"></script>
    <script src="/layui/layui.js"></script>

</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">
<div class="layui-body" style="padding:15px;top: 0px;">
    <link rel="stylesheet" href="/sb/stock.css"/>
<div class="sp-wrapper">
    <h2 class="sp-title"><span>采购退货详情</span></h2>
    <div class="sp-tool">
        <b style="color: red" th:text="${msg}"></b>
        <span class="fr">
            <i th:if="${invoice.checked ==0 && invoice.billStatus == 0}"  th:attr="erpInvoiceId=${invoice.getId()}"  href="javascript:;" class="purCheck save">审核通过</i>

            <i  id="save" class="save" th:if="${1== invoice.checked && invoice.billStatus == 1}">确认出库</i>
        </span>
    </div>
    <div class="sp-table">
        <form id="out_stock_from" action="/purchase/purchase_good_stock_out" method="post">
        <table>
            <thead>
            <tr>
                <th>序号</th>
                <th>图片</th>
                <th>商品名</th>
                <th>商品编码</th>
                <th>SKU</th>
                <th>数量</th>
                <th>已出库数量</th>
                <th>出库仓位</th>
                <th>出库数量</th>

            </tr>
            </thead>
            <tbody id="sp-data">
            <input type="hidden" name="StockInCheckoutId" id="StockInCheckoutId"  th:value="${outId}" />
            <tr th:each="item:${list}">
                <td th:text="${itemStat.index + 1}"></td>
                <td>
                    <img  width="50px" height="50px" />
                </td>
                <td ></td>
                
                <td th:text="${item.getGoodsNumber()}">
                <td th:text="${item.specNumber}"></td>
                <td th:text="${item.quantity}">200</td>
                <td th:text="${item.getInQuantity()}">200</td>
                <td>
                    <input type="hidden" name="outItemId"  th:value="${item.id}" />
                    <input type="hidden" name="maxQuantity"  th:value="${item.quantity - item.inQuantity}" />

                    <select class="out_stock" name="locationId" style="width:200px;">
                        <option th:each="stock:${item.getGoodStockList()}" th:value="${stock.getLocationId()}"
                                th:attr="keyong=${stock.currentQty - stock.lockedQty}"
                                th:text="${stock.getLocationName()+'  库存：'+stock.currentQty +'  锁定：'+ stock.lockedQty}"></option>
                    </select>
                </td>
                <td><input type="number" autocomplete="off" name="quantity" class="quantity" placeholder="出库数量" th:value="${item.quantity-item.getInQuantity()}"/></td>
            </tr>

            </tbody>
        </table>
        </form>
    </div>
    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>
    <script type="text/javascript">
        $(function () {
            // 保存
            $('#save').click(function () {
                var btnObj = $(this);
                var hasClick = $(this).hasClass("hasClick");
                if(hasClick == true){
                    alert("保存中，请勿重复点击")
                    return ;
                }
                btnObj.addClass("hasClick");
                var stockInItems = [];//要提交的入库数据
                var StockInCheckoutId = $("#StockInCheckoutId").val();
                var returnResult = true;//是否有错误数据
                var totalQuantity = 0;//总出库数量

                //检查仓位
                $(".out_stock").each(function (i,el) {
                    var locationId = $(this).val();
                    var keyong = $(this).find("option:selected").attr('keyong');//当前仓位可用
                    var locationNumber = $("input[name^='locationNumber']").eq(i).val();
                    var quantity = $("input[name^='quantity']").eq(i).val();

                    var StockInCheckoutItemId = $("input[name^='outItemId']").eq(i).val();

                    if (locationId != "" && locationId > 0 ) {
                        //选中入库，判断数量是否填写
                        if(quantity == "" || Number(quantity) < 0 ){
                            btnObj.removeClass("hasClick");
                            alert("请输入出库数量");
                            $("input[name^='quantity']").eq(i).focus();
                            returnResult = false;
                            return false;
                        }else{
                            //检查可入库数量
                            var maxQuantity = $("input[name^='maxQuantity']").eq(i).val();
                            if(Number(quantity) > Number(maxQuantity)){
                                btnObj.removeClass("hasClick");
                                alert("超出可出库数量");
                                $("input[name^='quantity']").eq(i).focus();
                                returnResult = false;
                                return false;
                            }else {
                                //判断可用库存
                                if(Number(keyong) <  Number(quantity)){
                                    btnObj.removeClass("hasClick");
                                    alert("可用库存不足");
                                    $("input[name^='quantity']").eq(i).focus();
                                    returnResult = false;
                                    return false;
                                }else {
                                    totalQuantity += Number(quantity);
                                    var item = {
                                        locationId: locationId,
                                        locationNumber: locationNumber,
                                        checkoutItemId: StockInCheckoutItemId,
                                        quantity: Number(quantity)
                                    }
                                    stockInItems.push(item);
                                }
                            }
                        }

                    }
                })

                if (returnResult == false) {
                    btnObj.removeClass("hasClick");
                    return false;
                }
                if (totalQuantity == 0) {
                    btnObj.removeClass("hasClick");
                    alert("没有要出库的商品");
                    return false;
                }

                $("#out_stock_from").submit();
                return false;
            });
            $('.purCheck').click(function () {
                var id = $(this).attr('erpInvoiceId');
                Tips.confirm({
                    title: '采购退货审核',
                    content: '您确定要审核通过吗？',
                    define: function () {
                        $.ajax({
                            url: "/ajax_purchase/purchase_return_check",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            data: JSON.stringify({erpInvoiceId: id,status:1}),
                            success: function (res) {
                                if (res.code == 0) {
                                    location.reload();
                                }
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                    }
                });
            });
        });
    </script>


</div>
</div>
</body>
</html>